﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;
using Cosmetic.DataAccess;
using System.Collections;

namespace Cosmetic.Logic
{
    public class BusinessManager : LogicBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="anBusinessInfo"></param>
        public void InsertBusinessInfo(BusinessInfo anBusinessInfo)
        {
            m_CosmeticDC.BusinessInfos.InsertOnSubmit(anBusinessInfo);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="anBusinessInfo"></param>
        public void DeleteBusinessInfo(BusinessInfo anBusinessInfo)
        {
            m_CosmeticDC.BusinessInfos.DeleteOnSubmit(anBusinessInfo);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="anUserId"></param>
        public void DeleteBusinessInfo(Guid anUserId)
        {
            this.DeleteBusinessInfo(this.GetBusinessInfoById(anUserId));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="anUserId"></param>
        /// <returns></returns>
        public BusinessInfo GetBusinessInfoById(Guid anUserId)
        {
            return m_CosmeticDC.BusinessInfos.FirstOrDefault(theBusinessInfo => theBusinessInfo.UserId == anUserId);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="BusinessNo"></param>
        /// <returns></returns>
        public BusinessInfo GetBusinessInfoByBusinessNo(string BusinessNo)
        {
            return m_CosmeticDC.BusinessInfos.FirstOrDefault(theBusinessInfo => theBusinessInfo.BusinessNo == BusinessNo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<BusinessInfo> GetAll()
        {
            return m_CosmeticDC.BusinessInfos.ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offsale"></param>
        public void InsertOffSale(OffSale offsale)
        {
            m_CosmeticDC.OffSales.InsertOnSubmit(offsale);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aStartDate"></param>
        /// <param name="aEndDate"></param>
        /// <returns></returns>
        public List<OffSale> GetAllbyDate(DateTime aStartDate, DateTime aEndDate)
        {
            if (aStartDate != null && aEndDate != null)
            {
                return (from theOffSale in m_CosmeticDC.OffSales
                        where theOffSale.CreateTime >= aStartDate &&
                        theOffSale.CreateTime <= aEndDate
                        select theOffSale).ToList();
            }
            else if (aStartDate != null && aEndDate == null)
            {
                return (from theOffSale in m_CosmeticDC.OffSales
                        where theOffSale.CreateTime >= aStartDate
                        select theOffSale).ToList();
            }
            else
            { return null; }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aBusinessNo"></param>
        /// <param name="aStartDate"></param>
        /// <param name="aEndDate"></param>
        /// <returns></returns>
        public List<OffSale> GetAllByDateByBusinessNo(string aBusinessNo, DateTime aStartDate, DateTime aEndDate)
        {
            if (aStartDate != null && aEndDate != null)
            {
                return (from theOffSale in m_CosmeticDC.OffSales
                        where theOffSale.BusinessNo == aBusinessNo &&
                        theOffSale.CreateTime >= aStartDate &&
                        theOffSale.CreateTime <= aEndDate
                        select theOffSale).ToList();
            }
            else if (aStartDate != null && aEndDate == null)
            {
                return (from theOffSale in m_CosmeticDC.OffSales
                        where theOffSale.BusinessNo == aBusinessNo &&
                        theOffSale.CreateTime >= aStartDate
                        select theOffSale).ToList();
            }
            else
            { return null; }


        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aBusinessNo"></param>
        /// <param name="aStartDate"></param>
        /// <param name="aEndDate"></param>
        /// <param name="aLevel"></param>
        /// <returns></returns>
        public List<OffSale> GetOffSaleByDateByLevel(string aBusinessNo, DateTime aStartDate, DateTime aEndDate, string aLevel)
        {
            if (aStartDate != null && aEndDate != null)
            {
                return (from theOffSale in m_CosmeticDC.OffSales
                        where theOffSale.BusinessNo == aBusinessNo &&
                        theOffSale.CreateTime >= aStartDate &&
                        theOffSale.CreateTime <= aEndDate &&
                        theOffSale.ProductLevel == aLevel
                        select theOffSale).ToList();
            }
            else if (aStartDate != null && aEndDate == null)
            {
                return (from theOffSale in m_CosmeticDC.OffSales
                        where theOffSale.BusinessNo == aBusinessNo &&
                        theOffSale.CreateTime >= aStartDate &&
                        theOffSale.ProductLevel == aLevel
                        select theOffSale).ToList();
            }
            else
            { return null; }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aMonth"></param>
        /// <returns></returns>
        public IList GetCountByMonth(DateTime aMonth)
        {
            var result = (from theOffSale in m_CosmeticDC.OffSales
                                  where theOffSale.CreateTime.Year == aMonth.Year &&
                                  theOffSale.CreateTime.Month == aMonth.Month
                                  group theOffSale by theOffSale.BusinessNo into newOffSale
                          select new
                          {
                              //BusinessUser = from o in m_CosmeticDC.OffSales where o.BusinessNo == newOffSale.Key select o.BusinessInfo.User.NickName,
                              BusinessNo = newOffSale.Key,
                              Count = newOffSale.Count(),
                              OffSalePrice = newOffSale.Sum(o=> o.OffSalePrice),
                              DiscountPrice =newOffSale.Sum(o=> o.DiscountPrice),
                          }).ToList();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string RandomBusinessNo()
        {  
           string strSep = ",";
           char[] chrSep = strSep.ToCharArray();

           //因0与O不容易分清楚，所以去除
           string strChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";

           string[] aryChar= strChar.Split( chrSep, strChar.Length );

           string strRandom = string.Empty;
           Random theRandom= new Random();
           
           //生成随机字符串
           for( int i=0; i < 7; i++ )
           {
            strRandom += aryChar[theRandom.Next( aryChar.Length )];
           }

           if (GetBusinessInfoByBusinessNo(strRandom) == null)
           {
               return strRandom;
           }
           else
           {
               return RandomBusinessNo();
           }
         
        }
    }
}